<?php
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

?>

<section class="admin__page-section">
    <div class="admin__page-section-title">
        <span class="title"><?php /* @escapeNotVerified */ echo __('Items to Ship') ?></span>
    </div>
    <div class="admin__table-wrapper">
        <table class="data-table admin__table-primary order-shipment-table">
            <thead>
                <tr class="headings">
                    <th class="col-product"><span><?php /* @escapeNotVerified */ echo __('Product') ?></span></th>
                    <th class="col-ordered-qty"><span><?php /* @escapeNotVerified */ echo __('Qty') ?></span></th>
                    <th class="col-qty<?php if ($block->isShipmentRegular()): ?> last<?php endif; ?>">
                        <span><?php /* @escapeNotVerified */ echo __('Qty to Ship') ?></span>
                    </th>
                    <?php if (!$block->canShipPartiallyItem()): ?>
                    <th class="col-ship last"><span><?php /* @escapeNotVerified */ echo __('Ship') ?></span></th>
                    <?php endif; ?>
                </tr>
            </thead>
            <?php $_items = $block->getShipment()->getAllItems() ?>
            <?php $_i = 0; foreach ($_items as $_item): if ($_item->getOrderItem()->getIsVirtual() || $_item->getOrderItem()->getParentItem()): continue; endif; $_i++ ?>
                <tbody class="<?php /* @escapeNotVerified */ echo $_i%2 ? 'odd' : 'even' ?>">
                    <?php echo $block->getItemHtml($_item) ?>
                    <?php echo $block->getItemExtraInfoHtml($_item->getOrderItem()) ?>
                </tbody>
            <?php endforeach; ?>
        </table>
    </div>
</section>

<section class="admin__page-section">
    <div class="admin__page-section-title">
        <span class="title"><?php /* @escapeNotVerified */ echo __('Shipment Total') ?></span>
    </div>
    <div class="admin__page-section-content order-comments-history">
        <div class="admin__page-section-item">
            <div class="admin__page-section-item-title">
                <span class="title"><?php /* @escapeNotVerified */ echo __('Shipment Comments') ?></span>
            </div>
            <div class="admin__page-section-item-content">
                <div id="order-history_form" class="admin__field">
                    <label class="admin__field-label"
                           for="shipment_comment_text">
                        <span><?php /* @escapeNotVerified */ echo __('Comment Text') ?></span></label>
                    <div class="admin__field-control">
                        <textarea id="shipment_comment_text"
                                  class="admin__control-textarea"
                                  name="shipment[comment_text]"
                                  rows="3"
                                  cols="5"><?php /* @escapeNotVerified */ echo $block->getShipment()->getCommentText(); ?></textarea>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="admin__page-section-item order-totals order-totals-actions">
        <div class="admin__page-section-item-title">
            <span class="title"><?php /* @escapeNotVerified */ echo __('Shipment Options') ?></span>
        </div>
        <div class="admin__page-section-item-content">
            <?php if ($block->canCreateShippingLabel()): ?>
                <div class="field choice admin__field admin__field-option field-create">
                    <input id="create_shipping_label"
                           class="admin__control-checkbox"
                           name="shipment[create_shipping_label]"
                           value="1"
                           type="checkbox"
                           onclick="toggleCreateLabelCheckbox();"/>
                    <label class="admin__field-label"
                           for="create_shipping_label">
                        <span><?php /* @escapeNotVerified */ echo __('Create Shipping Label') ?></span></label>
                </div>
            <?php endif ?>

            <div class="field choice admin__field admin__field-option field-append">
                <input id="notify_customer"
                       class="admin__control-checkbox"
                       name="shipment[comment_customer_notify]"
                       value="1"
                       type="checkbox"/>
                <label class="admin__field-label"
                       for="notify_customer">
                    <span><?php /* @escapeNotVerified */ echo __('Append Comments') ?></span></label>
            </div>

            <?php if ($block->canSendShipmentEmail()): ?>
                <div class="field choice admin__field admin__field-option field-email">
                    <input id="send_email"
                           class="admin__control-checkbox"
                           name="shipment[send_email]"
                           value="1"
                           type="checkbox"/>
                    <label class="admin__field-label"
                           for="send_email">
                        <span><?php /* @escapeNotVerified */ echo __('Email Copy of Shipment') ?></span></label>
                </div>
            <?php endif; ?>
            <?php echo $block->getChildHtml('submit_before') ?>
            <div class="order-history-comments-actions actions">
                <?php echo $block->getChildHtml('submit_button') ?>
                <?php echo $block->getChildHtml('submit_after') ?>
            </div>
        </div>
    </div>
</section>
<script>
require([
    "jquery",
    "Magento_Ui/js/modal/alert",
    "prototype"
], function(jQuery, alert){

//<![CDATA[
var sendEmailCheckbox = $('send_email');
if (sendEmailCheckbox) {
    var notifyCustomerCheckbox = $('notify_customer');
    var shipmentCommentText = $('shipment_comment_text');
    Event.observe(sendEmailCheckbox, 'change', bindSendEmail);
    bindSendEmail();
}
function bindSendEmail() {
    if (sendEmailCheckbox.checked == true) {
        notifyCustomerCheckbox.disabled = false;
    }
    else {
        notifyCustomerCheckbox.disabled = true;
    }
}
window.toggleCreateLabelCheckbox = function() {
    var checkbox = $('create_shipping_label');
    var submitButton = checkbox.up('.order-totals').select('.submit-button span')[0];
    if (checkbox.checked) {
        submitButton.innerText += '...';
    } else {
        submitButton.innerText = submitButton.innerText.replace(/\.\.\.$/, '');
    }
}
window.submitShipment = function(btn) {
    if (!validQtyItems()) {
        alert({
            content: '<?php /* @escapeNotVerified */ echo __('Invalid value(s) for Qty to Ship') ?>'
        });
        return;
    }
    var checkbox = $(btn).up('.order-totals').select('#create_shipping_label')[0];
    if (checkbox && checkbox.checked) {
        packaging.showWindow();
    } else {
        disableElements('submit-button');
        // Temporary solution will be replaced after refactoring order functionality
        jQuery('#edit_form').on('invalid-form.validate', function() {
            enableElements('submit-button');
            jQuery('#edit_form').off('invalid-form.validate');
        });
        jQuery('#edit_form').triggerHandler('save');
    }
}
window.validQtyItems = function() {
    var valid = true;
    $$('.qty-item').each(function(item) {
        var val = parseFloat(item.value);
        if (isNaN(val) || val < 0) {
            valid = false;
        }
    });
    return valid;
}

window.bindSendEmail = bindSendEmail;

window.shipmentCommentText = shipmentCommentText;
window.notifyCustomerCheckbox = notifyCustomerCheckbox;
window.sendEmailCheckbox = sendEmailCheckbox;
//]]>

});
</script>
